package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.IFormMapper;
import com.cuixichang.minimalism.transfer.dto.IFormDTO;
import com.cuixichang.minimalism.transfer.factory.IFormAssembler;
import com.cuixichang.minimalism.transfer.po.IForm;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 拓展表单功能表表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-18
 */
@Service
public class IFormServiceImpl implements IFormService {

    @Autowired
    private IFormMapper iFormMapper;

    @Override
    public IFormDTO selectById(Long id){
        IForm iForm = iFormMapper.selectById(id);
        return IFormAssembler.INSTANCT.convertDto(iForm);
    }

    @Override
    public IFormDTO selectOne(IFormDTO iFormDTO){
        Assert.notNull(iFormDTO,"参数为空,请检查");
        IForm iForm = IFormAssembler.INSTANCT.convertPo(iFormDTO);
        IForm result = iFormMapper.selectOne(new QueryWrapper<>(iForm));
        return IFormAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<IFormDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<IForm> results = iFormMapper.selectBatchIds(ids);
        return results.stream().map(IFormAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<IFormDTO> selectList(IFormDTO iFormDTO){
        return this.selectList(iFormDTO, (iForm, queryWrapper) -> {});
    }

    @Override
    public List<IFormDTO> selectList(IFormDTO iFormDTO, BiConsumer<IForm, QueryWrapper<IForm>> consumer){
        Assert.notNull(iFormDTO,"参数为空,请检查");
        IForm iForm = IFormAssembler.INSTANCT.convertPo(iFormDTO);
        QueryWrapper<IForm> queryWrapper = new QueryWrapper<>(iForm);
        if(Objects.nonNull(consumer)){
            consumer.accept(iForm, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<IForm> results = iFormMapper.selectList(queryWrapper);
        return results.stream().map(IFormAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<IFormDTO> selectPage(IFormDTO iFormDTO,long pageSize,long pageNo){
        return this.selectPage(iFormDTO, pageSize, pageNo, (iForm, queryWrapper) -> {});
    }

    @Override
    public Page<IFormDTO> selectPage(IFormDTO iFormDTO,long pageSize,long pageNo, BiConsumer<IForm, QueryWrapper<IForm>> consumer){
        Assert.notNull(iFormDTO,"参数为空,请检查");
        IForm iForm = IFormAssembler.INSTANCT.convertPo(iFormDTO);
        QueryWrapper<IForm> queryWrapper = new QueryWrapper<>(iForm);
        if(Objects.nonNull(consumer)) consumer.accept(iForm, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<IForm> page = iFormMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<IFormDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(IFormAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iFormMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iFormMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(IFormDTO iFormDTO){
        return deleteByCondition(iFormDTO, (iForm, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(IFormDTO iFormDTO, BiConsumer<IForm, QueryWrapper<IForm>> consumer){
        Assert.notNull(iFormDTO,"参数为空,请检查");
        IForm iForm = IFormAssembler.INSTANCT.convertPo(iFormDTO);
        QueryWrapper<IForm> queryWrapper = new QueryWrapper<>(iForm);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iForm, queryWrapper);
        }
        return iFormMapper.delete(queryWrapper);
    }

    @Override
    public IFormDTO insert(IFormDTO iFormDTO){
        return insertHasOption(iFormDTO, (IFormDTO) -> null);
    }

    @Override
    public IFormDTO insertHasOption(IFormDTO iFormDTO, Function<IFormDTO,IFormDTO> preOption){
        Assert.notNull(iFormDTO,"参数为空,请检查");
        iFormDTO.check();
        IForm iForm = IFormAssembler.INSTANCT.convertPo(iFormDTO);
        Long createLoginId = iForm.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iForm.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iForm.setCreateTime(localDateTime);
        iForm.setUpdateTime(localDateTime);
        Boolean isEnable = iForm.getIsEnable();
        if(Objects.isNull(isEnable)) iForm.setIsEnable(true);
        Integer sort = iForm.getSort();
        if(Objects.isNull(sort)) iForm.setSort(0);
        if(Objects.nonNull(preOption)){
            IFormDTO apply = preOption.apply(iFormDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iFormMapper.insert(iForm);
        Assert.isTrue(insert>0,"插入失败");
        return IFormAssembler.INSTANCT.convertDto(iForm);
    }

    @Override
    public int updateById(IFormDTO iFormDTO){
         return updateById(iFormDTO, (iForm, updateWrapper) -> {});
    }

    @Override
    public int updateById(IFormDTO iFormDTO, BiConsumer<IForm, UpdateWrapper<IForm>> consumer){
        Assert.notNull(iFormDTO,"参数为空,请检查");
        Long id = iFormDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        IForm iForm = IFormAssembler.INSTANCT.convertPo(iFormDTO);
        Long createLoginId = iForm.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iForm.setCreateLoginId(null);
        iForm.setUpdateLoginId(createLoginId);
        iForm.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<IForm> updateWrapper = new UpdateWrapper<IForm>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iForm,updateWrapper);
        return iFormMapper.update(iForm,updateWrapper);
    }
}
